package com.dingwen.treasure.file.controller;

import com.dingwen.treasure.base.pojo.vo.Result;
import com.dingwen.treasure.file.component.M3u8Component;
import com.dingwen.treasure.file.service.impl.MinioFileServiceImpl;
import com.dingwen.treasure.tkmybatis.controller.AbstractBaseCrudControllerTkMybatisImpl;
import com.dingwen.treasure.tkmybatis.dto.TreasureFileDto;
import com.dingwen.treasure.tkmybatis.entity.TreasureFile;
import com.dingwen.treasure.tkmybatis.vo.TreasureFileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 　TreasureFileController
 * 　@author dingwen
 * 　@date 2022/7/4
 */
@Api(tags = "文件API")
@RestController
@Slf4j
@RequestMapping("file")
public class TreasureFileCrudController
        extends AbstractBaseCrudControllerTkMybatisImpl<TreasureFile, TreasureFileDto, TreasureFileVo> {

    /**
     * minioFileService
     */
    @Resource
    private MinioFileServiceImpl minioFileService;


    /**
     * m3u8Component
     */
    @Resource
    private M3u8Component m3u8Component;

    /**
     * 文件上传
     *
     * @param multipartFile 文件
     * @param bucketName    存储桶名称
     * @return {@link Result}<{@link TreasureFile}>
     */
    @ApiOperation(value = "文件上传", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "multipartFile", value = "multipartFile", dataType = "MultipartFile"),
            @ApiImplicitParam(name = "bucketName", value = "存储桶名称", paramType = "query")
    })
    @PostMapping("minio/upload/{bucketName}")
    public Result<TreasureFile> upload(@RequestParam(value = "multipartFile") MultipartFile multipartFile,
                                       @PathVariable(value = "bucketName") String bucketName) {
        TreasureFile treasureFile = minioFileService.upload(
                multipartFile,
                TreasureFile.builder().bucketName(bucketName).build()
        );
        return success(treasureFile);
    }

    @ApiOperation(value = "文件下载")
    @ApiImplicitParam(name = "fileId", value = "文件下载", paramType = "path")
    @GetMapping("/minio/download/{fileId}")
    public void download(@PathVariable("fileId") Long fileId, HttpServletRequest request,
                         HttpServletResponse response) {
        minioFileService.download(fileId, request, response);
    }

    @ApiOperation(value = "预览")
    @ApiImplicitParam(name = "fileId", value = "预览", paramType = "path")
    @GetMapping("/minio//preview/{fileId}")
    public void preview(@PathVariable("fileId") Long fileId, HttpServletResponse response) {
        minioFileService.preview(fileId, response);
    }

    @ApiOperation(value = "删除文件", response = Result.class)
    @ApiImplicitParam(name = "fileId", value = "文件ID", paramType = "path")
    @DeleteMapping("minio/{fileId}")
    public Result<Void> remove(@PathVariable("fileId") Long fileId) {
        minioFileService.remove(fileId);
        return success();
    }


    /**
     * 上传视频(m3u8):
     *
     * @param multipartFile 文件
     * @return {@link Result}
     * @RequestPart 支持更复杂的参数类似：(@RequestPart("uploadFile") MultiPartFile uploadFile @RequestPart("jsonData") Person
     * person)
     * @RequestParam 当同时存在文件参数时仅支持字符串： @RequestPart("uploadFile") MultiPartFile uploadFile,@RequestParam("jsonData")
     * String jsonData
     */
    @ApiOperation(value = "视频上传（M3U8）", response = Result.class)
    @ApiImplicitParam(name = "multipartFile", value = "multipartFile", dataType = "MultipartFile")
    @PostMapping("minio/m3u8/upload")
    public Result<Void> uploadVideoForM3U8(@RequestPart("multipartFile") MultipartFile multipartFile) {
        m3u8Component.executeUpload(multipartFile);
        return success();
    }

}
